package com.woniuxy.shiro;

import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 罗蔚
 * @Date 2022/4/14 16:26
 * @Description TODO
 */
@Configuration
public class ShiroConfig {
    /**
     * 基于web环境，创建一个securityManager的bean对象
     * ChangeLog : 1. 创建 (22/04/14/0014 下午 2:09 [马宇航]);
     * @param myRealm
     * @return org.apache.shiro.web.mgt.DefaultWebSecurityManager
     */
    @Bean
    public DefaultWebSecurityManager securityManager(MyRealm myRealm){
        return new DefaultWebSecurityManager(myRealm);
    }
    /**
     * 这里是配置过滤器的注册！
     * ChangeLog : 1. 创建 (22/04/14/0014 下午 2:15 [马宇航]);
     * @param securityManager 上面的@Bean DefaultWebSecurityManager对象
     * @return org.apache.shiro.spring.web.ShiroFilterFactoryBean
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //先配置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //配置过滤器，自定义的jwt过滤器
        Map<String, Filter> filterMap = new HashMap<>();
        //注册一个名字叫做jwt的过滤器
        filterMap.put("jwt",new JWTFilter());
        shiroFilterFactoryBean.setFilters(filterMap);
        //配置过滤器链的生效 顺序
        Map<String,String> filterChainDefinitionMap = new HashMap<>();
        //过滤器链的过滤规则，url作为key，jwt作为值，底层会自动帮我根据jwt名字，去找到对应的过滤器

        /**
         * key：表示访问的链接，支持通配符，它是个表达式！
         * value表示 过滤器的类型名称；shiro自带了一堆过滤器！
         * 常用的有 anon ：匿名访问
         *         authc:需要认证后才能访问
         **/
        filterChainDefinitionMap.put("/login","anon");
        filterChainDefinitionMap.put("/**","jwt");
        filterChainDefinitionMap.put("/401","anon");
        //暂时先不写其他的规则
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }
    /**
     * 处理springboot和shiro之间的bug，引起的一个动态代理的bug
     * ChangeLog : 1. 创建 (22/04/14/0014 下午 2:42 [马宇航]);
     *
     * @return org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator
     */
    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }
    //开启切面相关的内容
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProces(){
        return new LifecycleBeanPostProcessor();
    }
}
